
package com.study.services;

import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.CompetitorsMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.UserMDAO;

@Service
@Transactional(rollbackOn = Exception.class)
public class CompetitorsService {
	@Autowired
	CompetitorsMapper mapper;
	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	UserMDAO udao;

	/**
	 * 带条件的分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param selects
	 * @param cusid 
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String name, String selects,String useid, Integer cusid) {
		String power = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("341")) {
			power = selects;
		}
		System.out.println("加到");
		String names = "%" + name + "%";
		String selects1 = "%" + selects + "%";
		System.out.println(selects + "竞争对手程度");
		System.out.println(name+"    "+selects1+"         "+useid +"       "+cusid);
		if ("6".equals(useid)) {
			return mapper.selectByWhere(power != null ? power.trim() : power, names,cusid);
		}else {
			return mapper.selectByWhereOne(power != null ? power.trim() : power, names,useid,cusid);
		}
	
		
	}

	/**
	 * 根据id查询需求
	 * 
	 * @param id
	 * @return
	 */
	public JSONObject selectByid(String id) {
		return mapper.selectByid(Integer.parseInt(id));
	}

	/**
	 * 新增
	 * 
	 * @param theme
	 * @param privoder
	 * @param custname
	 * @param opp
	 * @param degree
	 * @param content
	 * @param date
	 * @return
	 */
	public boolean insertcom(String company, String custid, String price, String oppid, String nengli, String other,
			String youshi, String lieshi, String celuv, String more,String useid) {

		 String name = udao.getfindUsreid(useid).getString("use_name");
		double prices = 0.0;
		if (price != null && price != "") {
			prices = Double.parseDouble(price);
		}
		boolean flag = mapper.insertcom(company, custid, prices, oppid, Integer.parseInt(nengli), other, youshi, lieshi, celuv,
				more);
		if (flag) {
			 String logMessage ="员工"+name+"在"+new Date()+"新增了竞争对手信息";
			 int cusid = Integer.parseInt(custid);
				logforcustDAO.insert(cusid, new Date(), Integer.parseInt(useid), 2202, logMessage, "");
		}
		return flag;
	}
/**
 * 修改数据传输
 * @param theme
 * @param privoder
 * @param custname
 * @param opp
 * @param degree
 * @param content
 * @param demid
 * @return
 */
	public boolean updatecompet(String cus,String company,String price,String nengli,String other
			,String youshi,String lieshi,String celuv,String more,String comid,String useid) {

		 String name = udao.getfindUsreid(useid).getString("use_name");
		double prices = 0.0;
		if (price != null && price != "") {
			prices = Double.parseDouble(price);
		}
		boolean flag = mapper.updatecompet(company, prices, nengli, other, youshi, lieshi,celuv,more,Integer.parseInt(comid));
		 String logMessage ="员工"+name+"在"+new Date()+"修改了  " +company + "竞争对手信息";
		 int cusid = Integer.parseInt(cus);
			logforcustDAO.insert(cusid, new Date(), Integer.parseInt(useid), 2202, logMessage, "");
		return flag;
	}
}
